package com.hzdesk.media.koreabatchmatvector.fragment.fragment_persenter;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzdesk.media.koreabatchmatvector.bean.ConfigFileBean;
import com.hzdesk.media.koreabatchmatvector.config.RBConfig;
import com.hzdesk.media.koreabatchmatvector.fragment.fragment_contract.SetContract;
import com.hzdesk.media.koreabatchmatvector.fragment.fragment_modle.SetModle;
import com.hzdesk.media.koreabatchmatvector.inter.NetworkRequestListener;
import com.hzdesk.media.koreabatchmatvector.request.NetworkRequest;
import com.hzdesk.media.koreabatchmatvector.utils.JsonUitl;
import com.hzdesk.media.koreabatchmatvector.utils.MyToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Response;

/**
 * Created by Administrator on 2018/6/22.
 * 杭州席媒科技有限公司  Developers  刘传华  15
 * 佛祖镇楼 bug辟易
 */

public class SetPersenter implements SetContract.ISetPersenter {
    private SetContract.ISetView iSetView;
    private SetContract.ISetModle iSetModle;
    private Context mContext ;
    public SetPersenter(SetContract.ISetView mSetView) {
        this.iSetView = mSetView;
        iSetModle = new SetModle();
    }
    @Override
    public void SonClickedendMessage(int pager) {
        iSetView.sendMessage(pager);
    }
    @Override
    public void initDate(EditText EtName,Context tContext) {
        mContext = tContext;
        iSetView.initDate();
        EtName.setOnFocusChangeListener(onFocusChangeListener);

    }
    // todo 点击管理配置文件 ===============================
    @Override
    public void ConfigFileManger(Context mContext) {
        NetworkRequest mNetworkRequest =   new NetworkRequest();
        mNetworkRequest.setmNetworkRequestListener(mNetworkRequestListener);
        mNetworkRequest.requestServerDate(mContext, RBConfig.DEVICE_ATTRIBUTE);
    }

    @Override
    public void SaveMeetName(Context mContext) {
       String meetName =  iSetView.getMeetName();
       if(TextUtils.isEmpty(meetName)){
           meetName = "视频批注会议01";      // 默认会议名称
       }
     //   iSetModle.SaveMeetName(mContext,meetName);
    }

    // 管理配置文件
    private NetworkRequestListener mNetworkRequestListener = new NetworkRequestListener(){
        @Override
        public void success(Response response) {
            try {
                String NetWorkResponse = response.body().string();
                final JSONObject jsonObject = new JSONObject(NetWorkResponse);
                int Code = JsonUitl.getParsinCode(jsonObject);
                Log.e("SetPersenter", "success: Code "+Code);
                if(Code == 100){
                    String normalDate = JsonUitl.getStringDateFeld(jsonObject);
                    Log.e("SetPersenter", "success normalDate: "+normalDate);
                    ConfigFileBean mConfigFileBean = new Gson().fromJson(normalDate,ConfigFileBean.class);
                    // todo 存储数据库
                    iSetModle.SaveConfigFileDate(mConfigFileBean,mContext);
                    // todo 展示会议名称
                    iSetView.meetName(mConfigFileBean.getMeetName());
                }else{
                    Log.e("SetPersenter", "success: rdjh  ");
                    MyToastUtils.ShortToaseShow(mContext,jsonObject.getString("data"));
                }
            } catch (Exception e) {}

        }
        @Override
        public void fail(String s) {
            Log.e("SetPersenter" ,"fail: "+s);
        }
    };


    private View.OnFocusChangeListener onFocusChangeListener=new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {                             // 此处为得到焦点时的处理内容
            } else {                                    // 此处为失去焦点时的处理内容
                iSetView.focusChange( v);
            }
        }
    };
}
